/*
https://leetcode-cn.com/problems/circular-array-loop/solution/huan-xing-shu-zu-shi-fou-cun-zai-xun-hua-0ay2/
 */
public class Solution457 {
    public boolean circularArrayLoop(int[] nums) {
        int n=nums.length;
        for (int i=0;i<n;i++){
            if (nums[i]==0){
                continue;
            }
            int slow=i,fast=next(nums,i);
            while (nums[slow]*nums[fast]>0 && nums[slow]*nums[next(nums,fast)]>0){
                if (slow==fast){
                    if (next(nums,slow)==slow){
                        break;
                    }else{
                        return true;
                    }
                }
                slow=next(nums,slow);
                fast=next(nums,next(nums,fast));
            }
            int t=i;
            while (nums[t]*nums[next(nums,t)]>0){
                int tt=t;
                t=next(nums,t);
                nums[tt]=0;
            }
        }
        return false;
    }
    public int next(int[] nums,int k){
        int n=nums.length;
        return (k+nums[k]%n+n)%n;
    }

    public static void main(String[] args) {
        System.out.println(new Solution457().circularArrayLoop(new int[]{2,-1,1,2,2}));
    }
}
